<template>
  <el-dialog v-model="visible" title="申请开票" width="800px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
    >
      <div class="title">发票抬头</div>
      <el-divider />
      <div style="display: flex; flex-direction: row; align-items: center">
        <div>请选择发票抬头：</div>
        <el-select
          v-model="invoiceTitleId"
          placeholder="请选择发票抬头"
          style="flex: 1; margin-right: 10px"
          @change="onChange"
          clearable
          filterable
        >
          <el-option
            v-for="item in invoiceList"
            :label="item.invoiceTitle"
            :value="item.id"
            :key="item.id"
          ></el-option>
        </el-select>
        <el-button type="primary" @click="addVisible = true"
          >新增开票信息</el-button
        >
      </div>
      <div class="tip">请填写仔细核对您的开票信息</div>
      <el-form-item label="开票抬头" prop="invoiceTitle">
        <el-input
          v-model="formData.invoiceTitle"
          placeholder="请输入开票抬头"
          disabled
        />
      </el-form-item>
      <el-form-item label="纳税识别号" prop="invoiceTin">
        <el-input
          v-model="formData.invoiceTin"
          placeholder="请输入纳税识别号"
          disabled
        />
      </el-form-item>
      <el-form-item label="地址" prop="invoiceAddress">
        <el-input
          v-model="formData.invoiceAddress"
          placeholder="请输入地址"
          disabled
        />
      </el-form-item>
      <el-form-item label="电话" prop="invoicePhone">
        <el-input
          v-model="formData.invoicePhone"
          placeholder="请输入电话"
          disabled
        />
      </el-form-item>
      <el-form-item label="开户行" prop="openingBank">
        <el-input
          v-model="formData.openingBank"
          placeholder="请输入开户行"
          disabled
        />
      </el-form-item>
      <el-form-item label="账号" prop="accountNumber">
        <el-input
          v-model="formData.accountNumber"
          placeholder="请输入账号"
          disabled
        />
      </el-form-item>
      <el-form-item label="接收邮箱" prop="invoiceEmail">
        <el-input
          v-model="formData.invoiceEmail"
          placeholder="请输入接收邮箱"
          disabled
        />
      </el-form-item>
      <el-form-item label="收件人+电话" prop="accountNumber">
        <el-input
          v-model="formData.recipientTelephone"
          placeholder="请输入收件人+电话"
          disabled
        />
      </el-form-item>
      <el-form-item label="收件人地址" prop="recipientAddress">
        <el-input
          v-model="formData.recipientAddress"
          disabled
          placeholder="请输入收件人地址"
        />
      </el-form-item>
      <div class="title">发票抬头</div>
      <el-divider />
      <el-form-item label="开票金额" prop="invoiceAmount">
        <el-input
          v-model="formData.invoiceAmount"
          placeholder="请输入开票金额"
          disabled
        >
          <template #append>
            {{ selectDictLabels(bus_currency_type, currencyType) }}
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="发票类型" prop="invoiceType">
        <el-select
          clearable
          v-model="formData.invoiceType"
          placeholder="请选择发票类型"
          style="width: 200px"
        >
          <el-option
            v-for="item in bus_invoice_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开票类型" prop="invoiceItem">
        <el-select
          clearable
          v-model="formData.invoiceItem"
          placeholder="请选择开票类型"
          style="width: 200px"
        >
          <el-option
            v-for="item in bus_invoice_item"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          placeholder="请输入备注"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button>取 消</el-button>
      </div>
    </template>
    <invoiceForm
      v-model:value="addVisible"
      id=""
      v-if="addVisible"
      @on-success="getList"
    ></invoiceForm>
  </el-dialog>
</template>
<script setup>
import { reactive } from "vue";
import { getAllInvoiceList, applyInvoice } from "@/api/invoice.js";
import { getUserInfo } from "@/api/login";
import invoiceForm from "../../invoiceMng/invoiceForm.vue";

const { proxy } = getCurrentInstance();

const { bus_invoice_type, bus_invoice_item, bus_currency_type } = proxy.useDict(
  "bus_invoice_type",
  "bus_invoice_item",
  "bus_currency_type"
);

const props = defineProps({
  value: {
    type: Boolean,
    default: false,
  },
  id: {
    type: String,
    default: "",
  },
  money: {
    type: String,
    default: "",
  },
});

const emits = defineEmits(["update:value", "on-success"]);

const visible = computed({
  get() {
    return props.value;
  },
  set(val) {
    emits("update:value", val);
  },
});

const formData = reactive({
  invoiceTitle: undefined,
  invoiceTin: undefined,
  invoiceAddress: undefined,
  invoicePhone: undefined,
  openingBank: undefined,
  accountNumber: undefined,
  invoiceEmail: undefined,
  recipientTelephone: undefined,
  recipientAddress: undefined,
  invoiceAmount: undefined,
  invoiceType: undefined,
  invoiceItem: undefined,
  remark: undefined,
});

const formRules = {
  invoiceTitle: [
    { required: true, message: "请输入开票抬头", trigger: "blur" },
  ],
  invoiceTin: [
    { required: true, message: "请输入纳税识别号", trigger: "blur" },
  ],
  invoiceEmail: [
    { required: true, message: "请输入接收邮箱", trigger: "blur" },
  ],
  invoiceAmount: [
    { required: true, message: "请输入开票金额", trigger: "blur" },
  ],
  invoiceType: [
    { required: true, message: "请选择发票类型", trigger: "change" },
  ],
  invoiceItem: [
    { required: true, message: "请选择开票类型", trigger: "change" },
  ],
};
const invoiceList = ref([]);
const currencyType = ref("");
const invoiceTitleId = ref("");
const formRef = ref();
const addVisible = ref(false);

function onChange(val) {
  const item = invoiceList.value.find((item) => item.id === val);
  Object.keys(formData).forEach((key) => {
    formData[key] = item[key];
  });
  formData.invoiceAmount = props.money;
}

async function getList() {
  try {
    const res = await getAllInvoiceList();
    console.log(res);
    invoiceList.value = res.data;
  } catch (error) {
    throw new Error(error);
  }
}

function submitForm() {
  if (!formRef.value) return;
  formRef.value.validate(async (valid) => {
    if (valid) {
      formData.customerRechargeId = props.id;
      await applyInvoice(formData);
      visible.value = false;
      proxy.useMessage().success("申请成功");
      emits("on-success");
    }
  });
}
function fetchUserInfo() {
  getUserInfo().then(({ data }) => {
    currencyType.value = data.currencyType;
  });
}
onMounted(() => {
  getList();
  fetchUserInfo();
});
</script>

<style scoped lang="scss">
.title {
  font-size: 16px;
  color: #000;
}
.tip {
  font-size: 12px;
  color: #f69c29;
  margin: 5px 10px;
}
</style>
